package com.apes.framework.plugin.fsm;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.outbox.annotation.OutBoxExclude;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Objects;

/**
 * 功能：状态流
 *
 * @author xul
 * @create 2020-08-27 11:37
 */
@OutBoxExclude
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FsmTransit.CONST_TABLE_NAME, indexes = {@Index(unique = true, columnList = "to_, from_, on_ ")})
public class FsmTransit extends BaseModel {
    public static final String CONST_TABLE_NAME = "fsm_transit";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "状态流：事件")
    @Comment("编码")
    private String id;

    @ManyToOne
    @JoinColumn(name = "from_", referencedColumnName = "id", nullable = false)
    @Comment("从...状态")
    private FsmState from;

    @ManyToOne(optional = false)
    @JoinColumn(name = "to_", referencedColumnName = "id", nullable = false)
    @Comment("到...状态")
    private FsmState to;

    @ManyToOne
    @JoinColumn(name = "on_", referencedColumnName = "id", nullable = false)
    @Comment("事件")
    private FsmEvent on;

    @Column(name = "when_mvel", length = 120)
    @Comment("转换条件")
    private String whenMvel;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("是否有效")
    private boolean valid = true;

    public void setFrom(FsmState from) {
//        if (to != null && from.getId().equals(to.getId())) {
//            throw new RuntimeException("开始状态和结束状态不允许为同一值！");
//        }
//        if(to != null && !to.getFsmFlow().equals(from.getFsmFlow())){
//            throw new RuntimeException("开始状态和结束状态必须属于同一个状态流！");
//        }
//        if (to.isFinish()) {
//            throw new RuntimeException("结束节点不允许为开始状态！");
//        }
        this.from = from;
    }

    public void setTo(FsmState to) {
//        if (from != null && from.getId().equals(to.getId())) {
//            throw new RuntimeException("开始状态和结束状态不允许为同一值！");
//        }
//        if(from != null && !from.getFsmFlow().getId().equals(to.getFsmFlow().getId())){
//            throw new RuntimeException("开始状态和结束状态必须属于同一个状态流！");
//        }
//        if (to.isStarted()) {
//            throw new RuntimeException("启动节点不允许为结束状态！");
//        }
        this.to = to;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FsmTransit transit = (FsmTransit) o;
        return Objects.equals(id, transit.id);
    }


}
